package com.gyj.register;

import com.gyj.register.entity.*;
import com.gyj.register.enums.OperationTypeEnum;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 发送http请求的组件
 *
 * @author Gao
 * @date 2021/6/26
 */
public class HttpSender {

    private static final Logger logger = LogManager.getLogger(HttpSender.class);

    public static final String SERVICE_NAME2 = "inventory-service";
    public static final String IP2 = "192.168.31.207";
    public static final String HOSTNAME2 = "inventory01";
    public static final int PORT2 = 9001;
    private static final Long HEARTBEAT_INTERVAL = 30 * 1000L;

    /**
     * 发送注册请求
     *
     * @param request
     * @return
     */
    public RegisterResponse register(RegisterRequest request) {
        // request中封装了服务名称、端口号、ip地址等信息
        logger.info("服务实例:{}, 发送请求进行注册...", request);
        // 收到请求之后封装一个response返回对象
        RegisterResponse response = new RegisterResponse();
        response.setStatus(RegisterResponse.SUCCESS);
        return response;
    }

    /**
     * 发送心跳请求
     *
     * @param request
     * @return
     */
    public HeartBeatResponse heartBeat(HeartBeatRequest request) {
        // request中封装了服务名称、服务实例id信息
        logger.info("服务实例:{}, 发送请求进行心跳续约...", request);
        // 收到请求之后封装一个response返回对象
        HeartBeatResponse response = new HeartBeatResponse();
        response.setStatus(RegisterResponse.SUCCESS);
        return response;
    }

    /**
     * 全量拉取服务注册表
     * @return
     */
    public Applications fetchFullRegistry() {
        Map<String, Map<String, ServiceInstance>> registry = new HashMap<>(16);
        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setHostName("finance-service-01");
        serviceInstance.setIp("192.168.31.1207");
        serviceInstance.setPort(9000);
        serviceInstance.setServiceInstanceId("FINANCE-SERVICE-192.168.31.207:9000");
        serviceInstance.setServiceName("FINANCE-SERVICE");

        Map<String, ServiceInstance> serviceInstances = new HashMap<>(8);
        serviceInstances.put("FINANCE-SERVICE-192.168.31.207:9000", serviceInstance);
        registry.put("FINANCE-SERVICE", serviceInstances);
        logger.info("拉取注册表：{}", registry);
        return new Applications(registry);
    }

    /**
     * 增量拉取服务注册表
     * @return
     */
    public DeltaRegistry fetchDeltaRegistry() {
        ConcurrentLinkedQueue<RecentlyChangedServiceInstance> recentlyChangedQueue = new ConcurrentLinkedQueue<>();
        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setHostName("order-service-01");
        serviceInstance.setIp("192.168.31.288");
        serviceInstance.setPort(9000);
        serviceInstance.setServiceInstanceId("ORDER-SERVICE-192.168.31.288:9000");
        serviceInstance.setServiceName("ORDER-SERVICE");
        // 最近变化的队列
        RecentlyChangedServiceInstance recentlyChangedItem = new RecentlyChangedServiceInstance(
                serviceInstance, System.currentTimeMillis(), OperationTypeEnum.REGISTER.getCode());
        recentlyChangedQueue.add(recentlyChangedItem);
        logger.info("拉取增量注册表，最近变化的队列信息recentlyChangedQueue:{}", recentlyChangedQueue);
        // 封装返回参数
        DeltaRegistry deltaRegistry = new DeltaRegistry(recentlyChangedQueue, 2L);
        logger.info("增量拉取服务注册表, 返回结果:{}", deltaRegistry);
        return deltaRegistry;
    }

    /**
     * 服务下线
     * @param serviceName
     * @param serviceInstanceId
     */
    public void cancel(String serviceName, String serviceInstanceId) {
        logger.info("服务下线serviceName:{}, serviceInstanceId:{}", serviceName, serviceInstanceId);
    }

}

